<?php	 	 

/**
 * This is the model class for table "laporan_lain_lain".
 *
 * The followings are the available columns in table 'laporan_lain_lain':
 * @property integer $id
 * @property string $jenis_laporan
 * @property string $nama_laporan
 * @property string $deskripsi_laporan
 * @property string $tanggal_laporan
 * @property string $satker
 * @property string $lampiran
 * @property string $status
 * @property integer $pembuat
 * @property string $tanggal_pembuatan
 *
 * The followings are the available model relations:
 * @property Users $pembuat0
 */
class LaporanLainLain extends CActiveRecord
{
	public $provinsi;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return LaporanLainLain the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'laporan_lain_lain';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('jenis_laporan, nama_laporan, tanggal_laporan, satker', 'required'),
			array('pembuat_laporan', 'numerical', 'integerOnly'=>true),
			array('jenis_laporan, nama_laporan, lampiran, status', 'length', 'max'=>255),
			array('satker', 'length', 'max'=>25),
			array('status', 'SWValidator'),
			array('deskripsi_laporan, tanggal_pembuatan', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, provinsi, jenis_laporan, nama_laporan, deskripsi_laporan, tanggal_laporan, satker, lampiran, status, pembuat_laporan, tanggal_pembuatan', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'pembuat0' => array(self::BELONGS_TO, 'Users', 'pembuat_laporan'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'jenis_laporan' => 'Jenis Laporan',
			'nama_laporan' => 'Nama Laporan',
			'deskripsi_laporan' => 'Deskripsi Laporan',
			'tanggal_laporan' => 'Tanggal Laporan',
			'satker' => 'Satker',
			'lampiran' => 'Lampiran',
			'status' => 'Status',
			'pembuat' => 'Pembuat_laporan',
			'provinsi' => 'Provinsi',
			'tanggal_pembuatan' => 'Tanggal Pembuatan',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$id_satker = Yii::app()->user->getState('satker');
		$criteria=new CDbCriteria;
		
		if(Yii::app()->user->checkAccess('satker')){
			$criteria->condition = ' satker='.$id_satker;
		}
		if(Yii::app()->user->checkAccess('wilayah')){
			$criteria->condition = ' (status="swLaporanLainLain/Siap"  OR status="swLaporanLainLain/Reviewulang") AND satker LIKE "'.$id_satker.'%"';
		}
		if(Yii::app()->user->checkAccess('eselon1')){
		if(!empty($this->provinsi)){
			$criteria->join = 'JOIN satker ON t.satker = satker.satker_kode JOIN provinsi ON satker.provinsi_id = provinsi.id';
			$criteria->condition = 'provinsi.id='.$this->provinsi.' AND (status="swLaporanLainLain/Kirimkeelselon1") ';
		}
		else{
		$criteria->join = 'JOIN satker ON t.satker = satker.satker_kode JOIN provinsi ON satker.provinsi_id = provinsi.id';
			$criteria->condition = 'status="swLaporanLainLain/Kirimkeelselon1" ';
		}
		}
		if(Yii::app()->user->checkAccess('lembaga')){
		if(!empty($this->provinsi)){
			$criteria->join = 'JOIN satker ON t.satker = satker.satker_kode JOIN provinsi ON satker.provinsi_id = provinsi.id';
			$criteria->condition = 'provinsi.id='.$this->provinsi.' AND (status="swLaporanLainLain/Approve") ';
		}
		else{
		$criteria->join = 'JOIN satker ON t.satker = satker.satker_kode JOIN provinsi ON satker.provinsi_id = provinsi.id';
			$criteria->condition = 'status="swLaporanLainLain/Approve"';
		}
		}
		if(Yii::app()->user->checkAccess('pemeriksa')){
		if(!empty($this->provinsi)){
			$criteria->join = 'JOIN satker ON t.satker = satker.satker_kode JOIN provinsi ON satker.provinsi_id = provinsi.id';
			$criteria->condition = 'provinsi.id='.$this->provinsi.' AND (status="swLaporanLainLain/Publish") ';
		}
		else{
		$criteria->join = 'JOIN satker ON t.satker = satker.satker_kode JOIN provinsi ON satker.provinsi_id = provinsi.id';
			$criteria->condition = 'status="swLaporanLainLain/Publish"';
		}	
		}
		

		$criteria->compare('id',$this->id);
		$criteria->compare('jenis_laporan',$this->jenis_laporan,true);
		$criteria->compare('nama_laporan',$this->nama_laporan,true);
		$criteria->compare('deskripsi_laporan',$this->deskripsi_laporan,true);
		$criteria->compare('tanggal_laporan',$this->tanggal_laporan,true);
		$criteria->compare('satker',$this->satker,true);
		$criteria->compare('lampiran',$this->lampiran,true);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('pembuat_laporan',$this->pembuat_laporan);
		$criteria->compare('tanggal_pembuatan',$this->tanggal_pembuatan,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function behaviors()
	{
	    return array(
	        'swBehavior'=>array(
	            'class' => 'application.extensions.simpleWorkflow.SWActiveRecordBehavior',
	        ),
	    );
	}
}